package net.alche.al.util.alches;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author：levi
 * @date：2019/1/10 17:08
 */
public class ToolSerializer {
    /** 序列化*/
    public  byte[] to(Object object)
    {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try
        {
            // 序列化
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            byte[] bytes = baos.toByteArray();
            return bytes;
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**反序列化*/
    public  Object from(byte[] bytes)
    {
        ByteArrayInputStream bais = null;
        try
        {
            // 反序列化
            bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            return ois.readObject();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }
}
